#include "Dagger.h"
#include "Resource.h"

Dagger::Dagger(LPDIRECT3DDEVICE9 d3ddv, LPDIRECT3DSURFACE9 BackBuffer, LPD3DXSPRITE SpriteHandler, float x, float y, bool isLeft) : Object(x, y)
{	 
	p_sprite = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[40]->getFileName()), 125, 0);

	this->pos_x = x;
	this->pos_y = y;
	this->pos_x_old = pos_x;
	this->pos_y_old = pos_y;

	width = p_sprite->getTexture()->getWidth();
	height = p_sprite->getTexture()->getHeight();

	if (isLeft == true)
	{
		this->isLeft = true;
		this->veloc_x = -DRAGGER_VX;
		p_sprite->setFrame(0,1);
		p_sprite->setIndex(0);
	} 
	else
	{
		this->isLeft = false;
		this->veloc_x = DRAGGER_VX;
		p_sprite->setFrame(1,2);
		p_sprite->setIndex(1);
	}
	
	obj_state = IS_ACTIVING;
}

Dagger::~Dagger()
{
	if (p_sprite != NULL)
	{
		delete p_sprite;
	}
}

void Dagger::render(int cameraX, int cameraY)
{
	if (obj_state != IS_REMOVE)
	{
		p_sprite->render(pos_x, pos_y, cameraX, cameraY);
	}
}

OBJECT_TYPE Dagger::getObjectType()
{
	return OBJECT_ITEM;
}


void Dagger::update(list<Object*>* obj_list, DxInput* input, float gameTime)
{
	switch(obj_state)
	{
	case IS_IDLE:
		break;

	case IS_ACTIVING:
		{
			width = p_sprite->getTexture()->getWidth();
			height = p_sprite->getTexture()->getHeight();
			
			pos_x += veloc_x * gameTime;

			attack(obj_list, gameTime);

			if (abs(pos_x - pos_x_old) > 250)
			{
				changeState(IS_REMOVE);
			}
		}
		break;

	}
	p_sprite->update(gameTime);

}

//void Dragger::updatePosition(list<Object*>* obj_list, DxInput* input, float gameTime, int pos_x, int pos_y)
//{
//	switch(obj_state)
//	{
//	case IS_IDLE:
//		
//		break;
//
//	case IS_ACTIVING:
//		this->pos_x = pos_x;
//		this->pos_y = pos_y;
//
//		p_sprite->update(gameTime);
//		countFrameRod = p_sprite->getIndexFrame();
//		if (countFrameRod == 2)
//		{
//			attack(obj_list, gameTime);
//		}
//		else if (countFrameRod >= 3)
//		{
//			p_sprite->setIndexFrame(0);
//
//			countFrameRod = 0;
//
//			changeState(IS_REMOVE);
//		}
//
//		break;
//
//
//	}
//
//	//p_sprite->update(gameTime);
//
//}

void Dagger::changeState(int state)
{
	obj_state = state;
	switch(state)
	{
	case IS_ACTIVING:		
		//veloc_x = DRAGGER_VX;
		break;	 

	case IS_REMOVE:
		break;
	}
}

void Dagger::attack(list<Object*>* obj_list, float time)
{
	Object *obj;
	list<Object*>::iterator i_obj;

	//Va cham Thong Thuong
	for (i_obj = obj_list->begin(); i_obj != obj_list->end(); i_obj++)
	{
		obj = *i_obj;

		if (obj->getObjectType() == OBJECT_ENEMY || obj->getObjectType() == OBJECT_ITEM)
		{ 
			Box box(this->pos_x,
				this->pos_y - this->height, 
				this->width, 
				this->height,
				veloc_x * time,
				0);

			Box block(obj->getPosX(),
				obj->getPosY() - obj->getHeight(),
				obj->getWidth(),
				obj->getHeight());

			Box boxphase = Collision::GetSweptBroadphaseBox(box);			 

			if (Collision::AABBCheck(boxphase, block))
			{
				switch (obj->getIDObjType())
				{
				case 11:	//ghost
					{
						if (obj->getObjState() == ES_ACTIVING)
						{
							obj->changeState(ES_DIED);
							this->changeState(IS_REMOVE);
						}
					}
					break;

				case 12:	//bat boss
					{
						if (obj->getObjState() == ES_ACTIVING)
						{
							obj->changeState(ES_DIED);
							this->changeState(IS_REMOVE);
						}
					}
					break;

				case 13:	//bat boss
				case 18:	//head boss
					{
						if (obj->getObjState() == ES_ACTIVING)
						{
							int blood = obj->getBlood();
							blood--;
							obj->setBlood(blood);
							if (obj->getBlood() == 0)
							{
								obj->changeState(ES_DIED);
							}
							else
							{
								obj->changeState(ES_PAUSE);
							}
						}
					}
					break;

				case 14:	//red bat enemy
					{
						if (obj->getObjState() == ES_ACTIVING)
						{
							obj->changeState(ES_DIED);
							
						}
					}
					break;

				case 15:	//red dragon enemy
					{
						if (obj->getObjState() == ES_ACTIVING)
						{
							obj->changeState(ES_DIED);
							
						}
					}
					break;

				case 16:	//stone knight
					{
						if (obj->getObjState() == ES_ACTIVING)
						{
							obj->changeState(ES_DIED);

						}
					}
					break;

					case 31: //1 Heart - candle
					{
						if (obj->getObjState() == IS_IDLE)
						{
							obj->changeState(IS_EXPLOSION); 
						}

					}
					break;

				case 32: //5 Heart - candle
					{
						if (obj->getObjState() == IS_IDLE)
						{
							obj->changeState(IS_EXPLOSION); 
						}

					}
					break;
				case 321: //5 Heart - pot
					{
						if (obj->getObjState() == IS_IDLE)
						{
							obj->changeState(IS_EXPLOSION); 
						}

					}
					break;

				case 33: //Whip - pot
					{
						if (obj->getObjState() == IS_IDLE)
						{
							obj->changeState(IS_EXPLOSION); 
						}

					}
					break;

				case 34: //Dagger - pot
					{
						if (obj->getObjState() == IS_IDLE)
						{
							obj->changeState(IS_EXPLOSION); 
						}

					}
					break;

				case 35: //400 pts - candle
					{
						if (obj->getObjState() == IS_IDLE)
						{
							obj->changeState(IS_EXPLOSION); 
						}

					}
					break;

				case 36: //700 pts - candle
					{
						if (obj->getObjState() == IS_IDLE)
						{
							obj->changeState(IS_EXPLOSION); 
						}

					}
					break;

				case 37: //1000 pts - candle
					{
						if (obj->getObjState() == IS_IDLE)
						{
							obj->changeState(IS_EXPLOSION); 
						}

					}
					break;

				case 38: //Holy water - candle
					{
						if (obj->getObjState() == IS_IDLE)
						{
							obj->changeState(IS_EXPLOSION); 
						}

					}
					break;

				case 39: //Holy cross - candle
					{
						if (obj->getObjState() == IS_IDLE)
						{
							obj->changeState(IS_EXPLOSION); 
						}

					}
					break;

				case 40: //Stop watch - candle
					{
						if (obj->getObjState() == IS_IDLE)
						{
							obj->changeState(IS_EXPLOSION); 
						}

					}
					break;

				case 42: //Axe - candle
					{
						if (obj->getObjState() == IS_IDLE)
						{
							obj->changeState(IS_EXPLOSION); 
						}

					}
					break;

				case 44: //Orb - candle
					{
						if (obj->getObjState() == IS_IDLE)
						{
							obj->changeState(IS_EXPLOSION); 
						}

					}
					break;
				}

			}//end if

		}//end if

	}//end for
}